﻿using AiMoWindows.Windows.Scripts.ViewModels;
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Rendering;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Rename;
using Microsoft.CodeAnalysis.Text;
using RoslynPad.Roslyn.Rename;
using System.Windows.Input;

namespace AiMoWindows.MainActions.ScriptActions.SubActions
{
    class NameMatchScriptAction(ActionsController owner)
    {
        readonly ActionsController owner = owner;
        public async Task RenameSymbolAsync(RenameSymbolDialog renameDialog)
        {
            Document document = owner.CurrentDocument;
            ISymbol? symbol = await RenameHelper.GetRenameSymbol(document, owner.CurrentSelectionSpan.Start).ConfigureAwait(true);
            if (symbol == null) return;
            owner.Editor.IsReadOnly = true;
            System.Windows.Point point = GetCaretPosition(owner.Editor);
            await renameDialog.ShowDialog((int)point.X, (int)point.Y, symbol.Name);
            owner.Editor.IsReadOnly = false;
            if (renameDialog.ShouldRename)
            {
                var newSolution = await Renamer.RenameSymbolAsync(document.Project.Solution, symbol, new SymbolRenameOptions(), renameDialog.SymbolName ?? string.Empty).ConfigureAwait(true);
                var newDocument = newSolution.GetDocument(owner.DocumentId);
                // TODO: possibly update entire solution
                owner.ScriptsHost.UpdateDocument(newDocument!);
            }
            owner.Editor.Focus();
        }
        TextMatchHighlightRenderer? textMatchHighlightRenderer;
        public void SetTextMatchHighlightRenderer(TextMatchHighlightRenderer textMatchHighlightRenderer)
        {
            this.textMatchHighlightRenderer = textMatchHighlightRenderer;
        }
        TextSpan currentSelectionSpan = default;
        public void ClearMarkText()
        {
            if (currentSelectionSpan != default && currentSelectionSpan != owner.CurrentSelectionSpan)
            {
                textMatchHighlightRenderer?.SetSpans(null);
                currentSelectionSpan = default;
            }
        }
        public async void OnDoubleClickAndMarkText(object sender, MouseButtonEventArgs e)
        {
            if (textMatchHighlightRenderer == null) return;
            SyntaxToken selectedToken = await owner.GetCurrentSelectSyntaxToken();
            string variableName = selectedToken.ToString();
            if (variableName == null) return;
            currentSelectionSpan = selectedToken.Span;
            SyntaxNode root = await owner.GetCurrentSyntaxRoot();

            string identifierName = selectedToken.ToString();
            List<TextSpan> spans = new List<TextSpan>();

            foreach (var token in root.DescendantTokens())
            {
                if (token.Text == identifierName && selectedToken != token)
                {
                    spans.Add(token.Span);
                }
            }
            textMatchHighlightRenderer.SetSpans(spans);
        }

        System.Windows.Point GetCaretPosition(TextEditor editor)
        {
            TextView textView = editor.TextArea.TextView;
            VisualLine visualLine = textView.GetVisualLine(editor.TextArea.Caret.Line);
            if (visualLine == null)
            {
                return new System.Windows.Point(10, 10);
            }
            // 获取光标的视觉位置
            System.Windows.Point caretPosition = textView.GetVisualPosition(
                editor.TextArea.Caret.Position,
                VisualYPosition.TextBottom);
            caretPosition.Y -= editor.VerticalOffset;
            if (caretPosition.Y < 10) caretPosition.Y = 10;
            if (caretPosition.Y > editor.ViewportHeight - 60) caretPosition.Y = editor.ViewportHeight - 60;
            if (caretPosition.X > editor.ViewportWidth - 150) caretPosition.X = editor.ViewportWidth - 150;

            // 转换为相对于编辑器的坐标
            return caretPosition;

        }

    }
}
